// pages/me/manageTeam/editTeamUser/editTeamUser.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ScopeUser:{
      userId:null,
      status:1,
      userTitle:'',
      roleList:[],
    },
    roleList:[
        {value:'viewCar',name:'查看车辆'},
        {value:'addCar',name:'发布车辆'},
        {value:'editCar',name:'编辑车辆'},
        {value:'closCar',name:'下架车辆'},
        {value:'manageWantBuy',name:'管理求购'},
        {value:'manageAuction',name:'管理拍卖'},
        {value:'manageSecondBatch',name:'管理一口价'},
        {value:'contactCustomer',name:'联系客户'},
        {value:'manageDealer',name:'管理公司'},
        {value:'manageTeam',name:'管理团队'},
        {value:'amountManage',name:'账户管理'},
        {value:'reportApplyAudit',name:'报告审批'}
    ],
    dfRole:[{
      name:'展厅经理',
      value:['viewCar','addCar','editCar','closCar','manageWantBuy','manageAuction','manageSecondBatch','contactCustomer','manageDealer','manageTeam','amountManage','reportApplyAudit']
    },{
      name:'内勤专员',
      value:['viewCar','addCar','editCar','closCar','manageWantBuy','manageAuction','manageSecondBatch','contactCustomer','amountManage','reportApplyAudit']
    },{
      name:'收车顾问',
      value:['viewCar','manageWantBuy','manageAuction','manageSecondBatch','contactCustomer']
    },{
      name:'金牌销售',
      value:['viewCar','addCar','editCar','closCar','manageAuction','manageSecondBatch','contactCustomer']
    },],
    dfRoleIndex:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(app.globalData.ScopeUser == null){
      app.showNoAuthOrNoGrant('缺少必要参数,请重试');
    }else{
      this.InitUserStatus();
    }
  },
  // 检查一下登录状态。
  async InitUserStatus(){
      app.showLoading("获取用户信息");
      await app.checkToken();
      this.InitUserInfo();
      wx.hideLoading();
  },
  InitUserInfo(){
      let userInfo = app.globalData.userInfo;
      // 这里再验证一遍权限。
      if (userInfo.roleList.indexOf('manageTeam') == -1) {
          app.showNoAuthOrNoGrant('您无权操作查看此页面');
          return;
      }else{
        this.setData({
          'ScopeUser': app.globalData.ScopeUser
        })
      }
  },
  publishInput(e){
    let ScopeUser = this.data.ScopeUser;
    ScopeUser[e.currentTarget.dataset.name] = e.detail.value;
    if(e.currentTarget.dataset.name == 'userTitle'){
      if(null != this.data.dfRoleIndex){
        this.setData({
          'ScopeUser':ScopeUser,'dfRoleIndex':null
        });
      }else{
        this.setData({
          'ScopeUser':ScopeUser
        });
      }
    }else{
      this.setData({
        'ScopeUser':ScopeUser
      });
    }
  },
  clickdfRole(e){
    let index = e.currentTarget.dataset.idx;
    let roleList = this.data.dfRole[index];
    if(this.changeUserRole({detail:{value:roleList.value}})){
      this.setData({
        'dfRoleIndex': index,
        'ScopeUser.userTitle':roleList.name
      })
    }
  },
  closeDfRole(e){
    var thisTime = e.timeStamp;
    var lastTime = this.data.lastTime;
    if (lastTime != 0) {
      if (thisTime - this.data.lastTime < 500){
        let roleList = [];
        this.changeUserRole({detail:{value:roleList}});
      }
    }
    this.setData({
      lastTime: thisTime
    })
  },
  changeUserRole(e){
      let roleList = e.detail.value;
      let ScopeUser = this.data.ScopeUser;
      ScopeUser.roleList = roleList;
      ScopeUser.roleObj = {'viewCar':0,'addCar':0,'editCar':0,'closCar':0,'manageWantBuy':0,'manageAuction':0,'manageSecondBatch':0,'contactCustomer':0,'manageDealer':0,'manageTeam':0};
      roleList.forEach(item =>{
        ScopeUser.roleObj[item] = 1
      })
      this.setData({
          'ScopeUser': ScopeUser,
          'dfRoleIndex': null
      });
      return true
  },
  // 保存更改。
  saveUser(e){
    let user = app.deepCopy(this.data.ScopeUser);
    if(user.userName == ''){
      app.alert('请输入成员名称');
      return;
    }
    if(user.userTitle == ''){
      app.alert('请输入职务名称');
      return;
    }
    if(user.roleList.length == 0){
      app.alert('请选择用户权限');
      return;
    }
    delete user.roleObj;
    let self = this;
    app.sHttp.dealerUserModify(user).then(res=>{
      wx.showModal({
        title: '成功',
        content: '修改用户成功',
        confirmText: '我知道啦',
        showCancel: false,
        success(res) {
          self.BackPage();
        }
      })
    },err=>{
      app.error(err.msg);
    });
  },
  BackPage() {
    var that = this;
    wx.navigateBack({
      delta: 1,
      fail: function(){
        that.toHome();
      }
    });
  },
  toHome(){
    wx.reLaunch({
      url: '/pages/home/home',
    })
  }
})